//
//  BaseTabBarViewController.m
//  MengCP
//
//  Created by Gideon on 16/4/27.
//  Copyright © 2016年 LiZhenning. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "BaseNavigationViewController.h"


@interface BaseTabBarViewController ()
{
    NSArray *arrayImages;
    NSArray *arrayImagesSelected;
    NSArray *arrayColor;
    NSMutableArray *arrayButtonsImageView;
    UIImageView *_tabBarView;
    
}

@end

@implementation BaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    arrayImages = @[@"icon_home",@"icon_zixun",@"icon_user",];
    arrayImagesSelected = @[@"icon_home_o",@"icon_zixun_o",@"icon_user_o"];
    
    arrayButtonsImageView = [[NSMutableArray alloc] initWithCapacity:0];
    
    [self initView];
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

+ (BaseTabBarViewController *)shareInstance
{
    
    static BaseTabBarViewController *instance = nil;
    
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        
        instance = [[BaseTabBarViewController alloc] init];
        
    });
    
    
    return instance;
}

- (void)initView
{
    self.tabBar.hidden = YES;
    
    HomeViewController * vc1 = [HomeViewController new];
    InformationViewController * vc2 = [InformationViewController new];
    MyViewController * vc3 = [MyViewController new];
    
    
    BaseNavigationViewController *nv1 = [[BaseNavigationViewController alloc] initWithRootViewController:vc1];
    BaseNavigationViewController *nv2 = [[BaseNavigationViewController alloc] initWithRootViewController:vc2];
    BaseNavigationViewController *nv3 = [[BaseNavigationViewController alloc] initWithRootViewController:vc3];


    self.viewControllers = [NSArray arrayWithObjects:nv1,nv2,nv3,nil];
    
    _tabBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kScreenHeight - TabbarHeight, kScreenWidth, 49)];
    [_tabBarView setUserInteractionEnabled:YES];
    [_tabBarView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_tabBarView];
    
    
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, _tabBarView.current_w, 0.5)];
    [line setBackgroundColor:HexRGBA(0xabb0b6, 1)];
    [_tabBarView addSubview:line];
    
    
    NSInteger count = [arrayImages count];
    
    CGFloat itemWidth = kScreenWidth / count;
    
    for (NSInteger i = 0; i < count; i ++)
    {
        UIButton *tabBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [tabBarButton setTag:i];
        [tabBarButton setFrame:CGRectMake(i * itemWidth, 0, itemWidth, 49)];
        [tabBarButton addTarget:self action:@selector(btnTabBarButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImage *image = [UIImage imageNamed:i==0?arrayImagesSelected[i]:arrayImages[i]];
        UIImageView *ivIcon = [[UIImageView alloc] initWithImage:image];
        [ivIcon setTag:999];
        [tabBarButton addSubview:ivIcon];
        [_tabBarView addSubview:tabBarButton];
        [arrayButtonsImageView addObject:ivIcon];
        [ivIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(tabBarButton);
            make.top.equalTo(tabBarButton).offset((49 - image.size.height)/2.0);
            make.size.mas_equalTo(CGSizeMake(image.size.width, image.size.height));
        }];
        
    }

    
    [self setIndexSelected:0];
    
}


- (void)btnTabBarButtonPressed:(id)sender
{
    
    
    UIButton *btn = (UIButton *)sender;
    NSInteger tag = btn.tag;
    
    [self setIndexSelected:tag];
    
}


- (void)setIndexSelected:(NSInteger)tag
{
    
    NSInteger count = [arrayImages count];
    
    for (NSInteger i = 0; i < count; i++)
    {
        UIImageView *ivIcon = arrayButtonsImageView[i];
        
        [ivIcon setImage:[UIImage imageNamed:i == tag ? arrayImagesSelected[i] : arrayImages[i]]];
        
    }
    
    self.selectedIndex = tag;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


@end
